﻿@{
    Layout = "~/Areas/Shared/_spTreeGrid.cshtml";
    ViewBag.AddWindowTitle = "添加产品分类";
    ViewBag.EditWindowTitle = "编辑产品分类";
}
<script>
    var grid;
    var _width = 400;
    var _height = 300;
    function AddZL(id,parentName) {
    
        stopPropagation();
 
 
        var url = '@Url.Action("AddCategory", "ProductCategoryManage", new { area = "Cubic" })?parentId=' + id + '&parentName=' + parentName;
        dialog = parent.openWindow({
            title: '@ViewBag.AddWindowTitle',
            url: url,
            height: _height,
            width: _width,
            isIframe: true,
            onClose: function () {
            },
            toolbar: [
                {
                    text: '保存',
                    iconCls: "icon-save",
                    handler: function () {
                        dialog.find("iframe")[0].contentWindow.saveData(function () {
                            dialog.dialog("close");
                            $("#DataTreeTb").treegrid('reload');
                        });
                    }
                },
                '-',
                {
                    text: '关闭',
                    iconCls: "icon-no",
                    handler: function () {
                        dialog.dialog("close");
                    }
                }
            ]
        });
    }

    function EditZL(Id) {
 
        var url = '@Url.Action("EditCategory", "ProductCategoryManage", new { area = "Cubic" })?Id=' + Id;
        dialog = parent.openWindow({
            title: '@ViewBag.EditWindowTitle',
            url: url,
            height: _height,
            width: _width,
            isIframe: true,
            onClose: function () {
            },
            toolbar: [
                {
                    text: '保存',
                    iconCls: "icon-save",
                    handler: function () {
                        dialog.find("iframe")[0].contentWindow.saveData(function () {
                            dialog.dialog("close");
                            $("#DataTreeTb").treegrid('reload');
                        });
                    }
                },
                '-',
                {
                    text: '关闭',
                    iconCls: "icon-no",
                    handler: function () {
                        dialog.dialog("close");
                    }
                }
            ]
        });
    }

    function del(Id) {
        var url = '@Url.Action("DelCategoryDetail", "ProductCategoryManage", new { area = "Cubic" })?ids=' + Id;
        $.messager.confirm('提示信息', '您确认要删除选中的所有记录吗?', function (data) {
            if (data) {
                $.ajax({
                 
                    url: url,
                    type: 'post',
                    error: function () {
                        Msgalert('错误', '删除失败!', 'error');
                        grid.treegrid('clearSelections');
                    },
                    success: function (Result) {
                        var data = eval('(' + Result + ')');
                        if (data.success) {
                            Msgfade(data.msg); //提示消息
                            $("#DataTreeTb").treegrid('reload');
                            $("#DataTreeTb").treegrid('clearSelections');
                        } else {
                            Msgalert('错误', data.msg, 'error');
                        }
                    }
                });
            }
        });

    }
    function Add() {
        var url = '@Url.Action("AddCategory", "ProductCategoryManage" )';
        dialog = parent.openWindow({
            title: '@ViewBag.AddWindowTitle',
            url: url,
            height: _height,
            width: _width,
            isIframe: true,
            onClose: function () {
            },
            toolbar: [
                {
                    text: '保存',
                    iconCls: "icon-save",
                    handler: function () {
                        dialog.find("iframe")[0].contentWindow.saveData(function () {
                            dialog.dialog("close");
                            $("#DataTreeTb").treegrid('reload');
                        });
                    }
                },
                '-',
                {
                    text: '关闭',
                    iconCls: "icon-no",
                    handler: function () {
                        dialog.dialog("close");
                    }
                }
            ]
        });
    }


    $(function () {
        grid = $("#DataTreeTb");
        $('#DataTreeTb').treegrid({
            idField: 'Id',
            treeField: 'name',
            url: "@Url.Action("GetCategoryTreeList", "ProductCategoryManage")",
            columns: [[
                { title: '分类名称', field: 'name', width: 280 },
                { title: '描述', field: 'CategoryDesc', width: 300, align: 'right' },
                { title: '更新时间', field: 'CreateTime', width: 120, formatter: Common.DateTimeFormatter },
                { title: '排序', field: 'Sequence', width: 40 },
                {
                    title: '操作', field: '操作', formatter: function (value, row) {
                        var arr = new Array();
 
                        arr.push('<a href=# class="OpertaionButton" onclick=AddZL("' + row.Id + '","' + encodeURIComponent(row.CategoryName) + '")>[添加子类]</a>');
                        arr.push('<a href=# class="OpertaionButton" onclick=EditZL("' + row.Id + '")>[修改]</a>');
                        arr.push('<a href=# class="OpertaionButton" onclick=del("' + row.Id + '")>[删除]</a>');
                        return arr.join('');
                    }

                }
            ]],
            toolbar: [{
                text: '新增根节点',
                iconCls: 'icon-add',
                handler: Add
            }]
        });

    });

</script>

<table id="DataTreeTb"></table>
